//时间
var formatTime = function (date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

var formatNumber = function (n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}


//根据时间计算岁数
function getAge(data) {
  if (data) {
    var now = getDate()
    return now.getFullYear() - data.slice(0, 4)
  }

}
function getQueryString(name, str) {
  var reg = new RegExp("(#)" + name + "=([^&]*)(&|$)");
  var r = str.match(reg) ? str.match(reg)[2] : null;

  if (r != null) return unescape(r);
  return getQueryString2(name);
}

function getQueryString2(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);

  if (r != null) return unescape(r[2]);
  return null;
}

function judgeBrand(sUserAgent) {
  var isIphone = sUserAgent.match(/iphone/i) == "iphone";
  var isHuawei = sUserAgent.match(/huawei/i) == "huawei";
  var isHonor = sUserAgent.match(/honor/i) == "honor";
  var isOppo = sUserAgent.match(/oppo/i) == "oppo";
  var isOppoR15 = sUserAgent.match(/pacm00/i) == "pacm00";
  var isVivo = sUserAgent.match(/vivo/i) == "vivo";
  var isXiaomi = sUserAgent.match(/mi\s/i) == "mi ";
  var isXiaomi2s = sUserAgent.match(/mix\s/i) == "mix ";
  var isRedmi = sUserAgent.match(/redmi/i) == "redmi";
  var isSamsung = sUserAgent.match(/sm-/i) == "sm-";

  if (isIphone) {
    return "//itunes.apple.com/us/app/yi-lan-ying-cai/id468319885?l=zh&ls=1&mt=8";
    // return "//1001.job1001.com/elanApp";
  } else if (isHuawei || isHonor) {
    return "//appgallery.huawei.com/#/app/C10184821?locale=zh_CN&source=appshare&subsource=C10184821&shareTo=weixin&shareFrom=appmarket";
  } else {
    return "//a.app.qq.com/o/simple.jsp?pkgname=com.elan.activity";
  }
}


function newList(arr){
  var map = {},
  dest = [];
  for(var i = 0; i < arr.length; i++){
      var ai = arr[i];
      if(!map[ai.uid]){
          dest.push({
              uid: ai.uid,
              cname: ai.cname_all,
              joblist: [ai]
          });
          map[ai.uid] = ai;
      }else{
          for(var j = 0; j < dest.length; j++){
              var dj = dest[j];
              if(dj.uid == ai.uid){
                  dj.joblist.push(ai);
                  break;
              }
          }
      }
  }
  return dest
}

function isPhone() {
  if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
      return true
    }
    else {
      return false
    }
}

function equipment(){
  let ua = navigator.userAgent.toLowerCase();
  if(ua.indexOf("micromessenger") != -1)return 'WeiXin';
  else if(ua.match(/iPhone|iPad|iPod|iOS|Android/i))return 'App'
  else {
    return 'H5'
  }
}

function checkPhone(value){
	let reg =/^1\d{10}$/
	if(reg.test(value)){
		return true;
	}
	return false
}
// 防抖
function debounce(fn, wait) {    
  var timeout = null;    
  return function() {        
      if(timeout !== null)   clearTimeout(timeout);        
      timeout = setTimeout(fn, wait);    
  }
}
function throttle(fn,gapTime){
  let lastTime=null
  return ()=>{
      let nowTime=+new Date() //转变为数字 
      if(nowTime-lastTime>gapTime){
          fn()
          lastTime=nowTime
      }            
  }
}

//清除所有cookie函数
function clearAllCookie() {
  var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
  if(keys) {
    for(var i = keys.length; i--;)
      document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
  }
}

const handle = {
  formatTime: formatTime,
  getAge: getAge,
  getQueryString,
  getQueryString2,
  judgeBrand,
  newList,
  isPhone,
  equipment,
  checkPhone,
  debounce,
  throttle,
  clearAllCookie
}


export default handle
